
import { SDKState } from "../SDKDefine";
import NativeAdItemModel from "./NativeAdItemModel";

export default class BaseNativeAd {


    state = SDKState.close;
    ads = [];
    adItems = []
    // instanceMap = []
    showFlag = false;
    adIndex = 0;

    constructor(ids) {
        this.ads = ids;
        // this.instanceMap = []
    }


    setState(s) {
        this.state = s;
    }

    open(adIndex, callback) {
        if (this.adItems.length > 0) {
            callback(this.adItems)
            this.adItems.length = 0;
        } else {
            this.state = SDKState.loading;
            this.callback = callback;
            console.log(" OppoNativeAd open")
            // this.destroy();
            this.create(adIndex);
            this.load()
        }


    }

    setShowFlag(flag) {
        this.showFlag = flag;
    }

    getShowFlag() {
        return this.showFlag;
    }

    getAdItems() {
        return this.adItems;
    }


    close() {
        this.callback = null;
    }


    onError(err) {
        this.setState(SDKState.loadFail)
        console.log(' BaseNativeAd onError err ', err)
        if (this.callback) {
            this.callback([])
        }
    }

    onLoad(res) {
        this.setState(SDKState.loadSucess)
        // console.log('onLoad ', res.adList)
        this.adItems.length = 0;
        if (res.adList) {
            for (let index = 0; index < res.adList.length; index++) {
                const element = res.adList[index];
                let adItem = new NativeAdItemModel()
                adItem.initWithOppo(element)
                this.reportAdShow(adItem.getID())
                this.adItems.push(adItem)
            }
            if (this.callback) {
                this.callback(this.adItems)
                this.adItems.length = 0;
                this.callback = null;
            }
        }
    }

    create(index) {
        this.adIndex = index;
        let adUnitID = this.ads[index];
        console.log(" OppoNativeAd create", index)
        this.destroy();
        // this.instance = this.instanceMap[index]
        if (!this.instance) {
            this.instance = qg.createNativeAd({
                adUnitId: adUnitID,
            });
            this.instance.onLoad(this.onLoad.bind(this))
            this.instance.onError(this.onError.bind(this))
            // this.instanceMap[index] = this.instance
        } else {
            // this.instance = this.instanceMap[index]
        }

    }

    load() {
        console.log(" OppoNativeAd load")
        if(this.instance){
            this.adItems.length = 0;
            this.instance.load();
        }

    }



    reportAdClick(adId) {
        if (!this.instance) {
            return
        }
        console.log('reportAdClick ', adId)
        this.instance.reportAdClick({
            adId: adId
        })
    }

    reportAdShow(adId) {
        if (!this.instance) {
            return
        }
        console.log('reportAdShow ', adId)
        this.instance.reportAdShow({
            adId: adId
        })
    }

    destroy() {
        if (this.instance) {
            this.instance.offLoad(this.onLoad.bind(this))
            this.instance.offError(this.onError.bind(this))
            if (this.instance && typeof (this.instance) == 'function')
                this.instance.destroy();
            // if (this.instanceMap[this.adIndex]) {
            //     this.instanceMap[this.adIndex] = null;
            // }
            this.instance = null;
        }

    }

}